
package com.huizhan.web.controller;
 
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.domain.Sort.*;
import io.swagger.annotations.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.*;
import java.io.File;
import java.util.*;

import com.huizhan.web.dao.*;
import com.huizhan.web.model.*;
import com.huizhan.web.view.*;

/**
 * 项目api
 */
 
@Api(tags = "Project",description = "项目")
@RestController
public class ProjectController {
    
    @Autowired
    private ProjectDao dao;
   
    @Autowired
    private ProjectTypeDao daoProjectType;

    /**
     * 获取项目集合
     * @return api结果
     */
    @ApiOperation(value = "获取项目集合")
    @RequestMapping(value =  "/api/Project/",method = {RequestMethod.GET})
    public ApiResult<List<Project>> Get(){
         ApiResult<List<Project>> result=new ApiResult<List<Project>>();
        try 
        {
            List<Project> res = dao.findAll();
            result.setData(res);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 根据项目类型ID获取项目集合
     * @param id 项目类型ID
     * @return api结果
     */
    @ApiOperation(value = "根据项目类型ID获取项目集合")
    @RequestMapping(value =  "/api/Project/ProjectType/{projectTypeId}",method = {RequestMethod.GET})
    public ApiResult<List<Project>> Get(@PathVariable Long projectTypeId){
        ApiResult<List<Project>> result=new ApiResult<List<Project>>();
        try
        {
            List<Project> model= dao.findByProjectTypeId(projectTypeId);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 更新状态
     * @param model 项目信息
     * @return api结果
     */
    @ApiOperation(value = "更新状态")
    @RequestMapping(value =  "/api/Project/State",method = {RequestMethod.PATCH})
    public ApiResult<State> State(@RequestBody State model){
         ApiResult<State> result=new ApiResult<State>();
         
        try
        {    
            Project account=dao.getOne(model.getId());

            if(account==null){
                result.setHasError(true);
                result.setMessage("没有该用户");
            }else{
                account.setEnabled(model.getEnabled());
                
                dao.save(account);
            }

            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
    
    /**
     * 根据ID获取项目信息
     * @param id 项目ID
     * @return api结果
     */
    @ApiOperation(value = "根据ID获取项目信息")
    @RequestMapping(value =  "/api/Project/{id}",method = {RequestMethod.GET})
    public ApiResult<Project> Get(@PathVariable String id){
        ApiResult<Project> result=new ApiResult<Project>();
        try
        {
            Project model= dao.getOne(id);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 修改项目信息
     * @param id 项目ID
     * @param model 项目信息
     * @return api结果
     */
    @ApiOperation(value = "修改项目信息")
    @RequestMapping(value =  "/api/Project/{id}",method = {RequestMethod.PUT})
    public ApiResult<Project> Edit(@PathVariable String id,@RequestBody Project model){
        ApiResult<Project> result=new ApiResult<Project>();
        try
        {
            Project project=dao.findByName(model.getName());

            if(project!=null&&!project.getProjectId().equals(id)){
                result.setHasError(true);
                result.setMessage("该项目名称已经存在");
            }else{
                project=dao.getOne(id);

                if(project!=null){
                    model.setAppId(project.getAppId());
                    model.setAppSecret(project.getAppSecret());
                    model.setPaySecret(project.getPaySecret());
                    model.setMerchantId(project.getMerchantId());
                    model.setCreateDate(new Date());
                    model.setProjectId(id);
                    dao.save(model);
                }else{
                    result.setHasError(true);
                    result.setMessage("参数错误");
                }
            }
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }


    /**
     * 上传校验文件.txt
     * @return api结果
     */
    @ApiOperation(value = "上传校验文件.txt")
    @RequestMapping(value =  "/api/Project/UploadTxt",method = {RequestMethod.POST})
    public ApiResult<String> UploadTxt(@RequestParam(value = "txt校验文件", required = true) MultipartFile files, HttpServletRequest request){
        ApiResult<String> result=new ApiResult<String>();
        try
        {
            String[] strings= files.getOriginalFilename().split("\\\\");
            String newFilePath= strings[strings.length-1];
            String c=request.getServletContext().getRealPath("")+newFilePath;
            File  file = new File(c);

            if(!file.exists()){ 
                files.transferTo(file);
            }
            if(request.getServerPort()!=80){
                result.setData(request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() +"/"+ newFilePath); 
            }else{
                result.setData(request.getScheme() + "://" + request.getServerName() + "/"+ newFilePath); 
            }
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    /**
     * 修改公众号信息信息
     * @param model 项目信息
     * @return api结果
     */
    @ApiOperation(value = "修改公众号信息信息")
    @RequestMapping(value =  "/api/Project",method = {RequestMethod.PUT})
    public ApiResult<Object> Project(@RequestBody Project model){
        ApiResult<Object> result=new ApiResult<Object>();
        try
        {
            Project project=dao.getOne(model.getProjectId());
            if(project!=null){
                project.setAppId(model.getAppId());
                project.setAppSecret(model.getAppSecret());
                project.setPaySecret(model.getPaySecret());
                project.setMerchantId(model.getMerchantId());

                dao.save(project);
            }
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 查询项目信息
     * @param model 账号信息
     * @return api结果
     */
    @ApiOperation(value = "查询项目信息")
    @RequestMapping(value =  "/api/Project/Search",method = {RequestMethod.POST})
    public ApiResult<List<Project>> Search(@RequestBody PageType model){
         ApiResult<List<Project>> result=new ApiResult<List<Project>>();
         
        try
        {    
            PageRequest pageable = PageRequest.of(model.getPageIndex(),model.getPageSize(),Direction.DESC,"project_Id") ;            
            Page<Project> page = dao.findByWhere(model.getName(),model.getType(),pageable);
            result.setData(page.getContent());
            result.setTotal(page.getTotalElements());
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    /**
     * 添加项目信息
     * @param model 项目信息
     * @return api结果
     */
    @ApiOperation(value = "添加项目信息")
    @RequestMapping(value =  "/api/Project/",method = {RequestMethod.POST})
    public ApiResult<Project> Add(@RequestBody Project model){
         ApiResult<Project> result=new ApiResult<Project>();
        try
        {
            Project project=dao.findByName(model.getName());

            if(project!=null){
                result.setHasError(true);
                result.setMessage("该项目名称已经存在");
            }else{
                model.setProjectId(UUID.randomUUID().toString());
                model.setCreateDate(new Date());
                dao.save(model);
                result.setData(model);
            }
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
    

    /**
     * 获取项目类型/项目二级树
     * @return api结果
     */
    @ApiOperation(value = "获取项目类型/项目二级树")
    @RequestMapping(value =  "/api/Project/Tree/",method = {RequestMethod.GET})
    public ApiResult<List<ProjectRoleTree>> Tree(){
        ApiResult<List<ProjectRoleTree>> result=new ApiResult<List<ProjectRoleTree>>();
        try
        {
           List<ProjectType> list= daoProjectType.findAll();
           List<Project> ps= dao.findAll();
           List<ProjectRoleTree> data=new ArrayList<ProjectRoleTree>();

            for (ProjectType projectType : list) {
                ProjectRoleTree tree=new ProjectRoleTree(projectType);
                for (Project p : ps) {
                    if(p.getProjectTypeId()==projectType.getProjectTypeId()){
                        ProjectTree tree1=new ProjectTree(p);
                        tree.getChildren().add(tree1);
                    }
                }
                data.add(tree);
            }
            result.setData(data);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 删除项目信息
     * @param id 项目ID
     * @return api结果
     */
    @ApiOperation(value = "删除项目信息")
    @RequestMapping(value =  "/api/Project/{id}",method = {RequestMethod.DELETE})
    public ApiResult<Object> Delete(@PathVariable  String id){
        ApiResult<Object> result=new ApiResult<Object>();
        try 
        {
            dao.deleteById(id);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    
}

